package com.ywk.base.toolClass.scriptEngine.apache;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 参考：https://blog.csdn.net/u013675821/article/details/141441602
 * 模板引擎
 * @Author zbh
 * @Date 2025/4/18
 */
public class TemplateEngine {
    public static void main(String[] args) {
//        stringExample();
//        objectExample();
        functionExample();
    }

    /**
     * 普通字符串
     */
    public static void stringExample(){
        Velocity.init();
        VelocityContext context = new VelocityContext();
        context.put("tableName", "users");

        List<String> columns = Arrays.asList("id", "username", "email", "create_time");
        context.put("columns", columns);

        String template = "SELECT\n" +
                "            #foreach($col in $columns)\n" +
                "                $col#if($foreach.hasNext), #end\n" +
                "            #end\n" +
                "            FROM ${tableName}\n" +
                "            WHERE status = 'ACTIVE'";

        StringWriter writer = new StringWriter();
        Velocity.evaluate(context, writer, "SqlTemplate", template);

        System.out.println(writer.toString());
    }

    /**
     * 实体对象值
     */
    public static void objectExample(){
        Velocity.init();

        VelocityContext context = new VelocityContext();
        User user = new User("李四", "lisi@example.com");
        context.put("user", user);

        String template = "用户信息:" +
                "姓名: ${user.name}" +
                "邮箱: ${user.email}";

        StringWriter writer = new StringWriter();
        Velocity.evaluate(context, writer, "UserTemplate", template);

        System.out.println(writer);
    }
    public static class User {//必须是public修饰的类
        private String name;
        private String email;

        public User(String name, String email) {
            this.name = name;
            this.email = email;
        }

        // getters
        public String getName() { return name; }
        public String getEmail() { return email; }
    }

    /**
     * 工具类方法
     */
    public static void functionExample() {
        VelocityContext context = new VelocityContext();
        context.put("dateTool", new DateTool());
        context.put("currentDate", new Date());

        String template = "当前日期: $dateTool.format($currentDate, \"yyyy-MM-dd\")\n" +
                "当前时间: $dateTool.formatNow(\"HH:mm:ss\")";

        StringWriter writer = new StringWriter();
        Velocity.evaluate(context, writer, "UserTemplate", template);

        System.out.println(writer);
    }

    public static class DateTool {
        public static String format(Date date, String pattern) {
            if (date == null) return "";
            return new SimpleDateFormat(pattern).format(date);
        }

        public static String formatNow(String pattern) {
            return format(new Date(), pattern);
        }
    }
}
